﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using PH.Model.Accounts;
using PH.Data.Accounts;
namespace PH.Logic.Accounts
{
    public class UserLogic
    {
        public static UserModel CreateNewUser(UserModel user)
        {
            user.Password = Utils.Encrypt.MD5Encrypt(user.Password);
            return UserCommand.CreateNewUser(user);
        }

        public static void UpdateUser(UserModel user)
        {
            UserCommand.UpdateUser(user);
        }

        public static UserModel GetUser(Guid guid)
        {
            return UserCommand.GetUser(guid);
        }

        public static UserModel GetUser(string username,bool getRoles=false)
        {
            return UserCommand.GetUser(username, getRoles);
        }

        public static UserModel GetUserByEmail(string email)
        {
            return UserCommand.GetUserByEmail(email);
        }

        public static UserModel GetUserByToken(Guid token)
        {
            return UserCommand.GetUserByToken(token);
        }

        public static UserModel Login(string userName, string password,bool pmd5=false)
        {
            var md5 = password;
            if (!pmd5) md5 = Utils.Encrypt.MD5Encrypt(password);
            return UserCommand.Login(userName, md5);
        }

        public static void RequestPasswordModify(Guid guid)
        {
            UserCommand.RequestPasswordModify(guid);
        }

        public static bool ResetPassword(Guid guid, string newPassword)
        {
            if (Utils.Password.CheckNumChar(newPassword, 8))
            {
                var md5New = Utils.Encrypt.MD5Encrypt(newPassword);
                return UserCommand.ResetPassword(guid, md5New);
            }
            else
                throw new Utils.PhException("新的密码必须至少8位字符（字母+数字组成）");
        }

        public static void ChangeEmail(Guid guid, string email)
        {
            UserCommand.ChangeEmail(guid, email);
        }
    
        #region membership

        public static UserCollection FindUsersInRole(string roleName, string usernameToMatch)
        {
            return UserCommand.FindUsersInRole(roleName, usernameToMatch);
        }

        public static UserCollection GetUsersInRole(string roleName)
        {
            return UserCommand.GetUsersInRole(roleName);
        }

        public static bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            if (Utils.Password.CheckNumChar(newPassword, 8))
            {
                var md5Old = Utils.Encrypt.MD5Encrypt(oldPassword);
                var md5New = Utils.Encrypt.MD5Encrypt(newPassword);
                return UserCommand.ChangePassword(userName, md5Old, md5New);
            }
            else
                throw new Utils.PhException("新的密码必须至少8位字符（字母+数字组成）");
        }

        public static bool IsUserInRole(string username, string roleName)
        {
            return UserCommand.IsUserInRole(username, roleName);
        }

        public static void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            UserCommand.RemoveUsersFromRoles(usernames, roleNames);
        }
        #endregion
    }
}
